class Solution {
public:
    int minimumOperations(string num) {
        //25 50 75 100 125 150 175 200 225
        int n = num.size();
        bool f1 = 0;
        int idx1 = 0, idx11 = 0;
        bool f2 = 0, f3 = 0, f4 = 0;
        int idx2 = 0, idx22 = 0;
        for (int i = n - 1; i >= 0; i--) {
            if (num[i] == '0') {
                f1 = 1;
                idx1 = i;
                break;
            }
        }
        for (int i = n - 1; i >= 0; i--) {
            if (num[i] == '5') {
                f3 = 1;
                idx2 = i;
                break;
            }
        }
        if (f1) {
            for (int i = idx1 - 1; i >= 0; i--) {
                if (num[i] == '5' || num[i] == '0') {
                    idx11 = i;
                    f2 = 1;
                    break;
                }
            }
        }
        if (f3) {
            for (int i = idx2 - 1; i >= 0; i--) {
                if (num[i] == '2' || num[i] == '7') {
                    idx22 = i;
                    f4 = 1;
                    break;
                }
            }
        }
        //12321
        cout << idx1 << " " << idx11 << endl;
        cout << idx2 << " " << idx22 << endl;
        cout << "f1==" << f1 << endl;
        cout << "f2==" << f2 << endl;
        cout << "f3==" << f3 << endl;
        cout << "f4==" << f4 << endl;
        int x = (n - 1) - idx1;
        int y = idx1 - idx11 - 1;
        int xx = (n - 1) - idx2;
        int yy = idx2 - idx22 - 1;
        int z = min(x + y, xx + yy);
        if (f1 && f2 && f3 && f4)return z;
        else if (f1 && f2) {
            if (num[idx11] != '0')return x + y;
            else {
                if (idx11 != 0)return x + y;
                else return n - 1;
            }
        }
        else if (f3 && f4) {
            cout << "chen" << endl;
            return xx + yy;
        }
        else if (f1) {
            return n - 1;
        }
        else if (f3) {
            return n;
        }
        return n;
    }
};